$(function () {
    // 获取上一个页面传过来的id
    let id = location.search.split('=')[1]
    // console.log(id)
    let fromdata = new FormData()
    if (id) {
        $('.btn-release').text('修改')
        fromdata.append('id', id)
        $.ajax({
            // url: BigNew.article_query,/article/search
            url: BigNew.article_search,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: { id },
            dataType: "json",
            success: function (res) {
                console.log(res)
                $('#inputTitle').val(res.data.title)
                $('.article_cover').attr('src', res.data.cover)
                setTimeout(function () {
                    $('.category').val(res.data.categoryId)
                    // $('.category option').eq(res.data.categoryId - 1).attr("selected", true)
                }, 200)
                $('#indate').val(res.data.date)
                // $('#mytextarea').val(res.data.content)
                $('#mytextarea').text(res.data.content)
                // $('#mytextarea').html(res.data.content)
            }
        });
    }


    //遍历文章类别
    $.ajax({
        url: BigNew.category_list,
        headers: { Authorization: localStorage.getItem('bignews_token') },
        dataType: "json",
        success: function (res) {
            // console.log(res)
            $('.category').html(template('sortTemp', res))
        }
    });


    //时期插件
    jeDate("#indate", {
        isinitVal: true,
        festival: true,
        format: 'YYYY-MM-DD',
        // maxDate: new Date().toLocaleDateString(),
        theme: { bgcolor: "#1378f0", color: "#ffffff", pnColor: "#00CCFF" },
    });

    //富文本框插件
    tinymce.init({
        selector: '#mytextarea',
        language: 'zh_CN',
        height: 300, //编辑器高度
        min_height: 200,
        plugins: [
            'advlist autolink lists link image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen',
            'insertdatetime media table contextmenu paste imagetools wordcount',
            'code'
        ],
        toolbar:
            'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code'
    });

    //文件选中时触发
    $('#inputCover').on('change', function () {
        let myfile = $('#inputCover')[0].files[0]
        if (myfile.type.indexOf('image/') == -1) {
            alert('只能选择图片作为封面')
            $('#inputCover').val('')
            return
        }
        let myurl = URL.createObjectURL(myfile)
        // console.log(myurl)
        $('.article_cover').prop('src', myurl)
    })

    //文章发布或存为草稿
    function articlesPubl(state) {

        //标题数据
        let title = $('#inputTitle').val().trim()

        if (!title) {
            alert('请输入标题')
            return
        }

        //封面数据
        let myfile = $('#inputCover')[0].files[0]
        // console.log(myfile)
        if (!myfile && !id) {
            alert('请选择封面图片')
            return
        }
        if (!id) {
            fromdata.append('cover', myfile)
        }
        if (myfile && id) {
            fromdata.append('cover', myfile)
        }

        // 获取id
        let categoryId = $('.category').val()
        // console.log(categoryId)

        // 获取时间
        let date = $('#indate').val()
        // console.log(date)

        //获取文章内容
        let content = tinymce.activeEditor.getContent()
        // console.log(content)
        if (!content) {
            alert('请输入文章内容')
            return
        }
        fromdata.append('title', title)
        // fromdata.append('cover', myfile)
        fromdata.append('categoryId', categoryId)
        fromdata.append('date', date)
        fromdata.append('content', content)
        fromdata.append('state', state)

        $.ajax({
            type: "post",
            //
            url: id ? BigNew.article_edit : BigNew.article_publish,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: fromdata,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (res) {
                // console.log(res)
                if (res.code == 200) {
                    alert(res.msg)
                    location.href = './article_list.html'
                    window.parent.$('.level02 li').eq(0).addClass('active').siblings().removeClass('active')
                }
            }
        });
    }

    //发布
    $('.btn-release').on('click', function (e) {
        e.preventDefault()
        // console.log(1)
        articlesPubl('已发布')

    })


    //草稿
    $('.btn-draft').on('click', function (e) {
        e.preventDefault()
        // console.log(2)
        articlesPubl('草稿')
    })

})